<!--
 * @Author: actor
 * @Date: 2023-11-13 15:42:28
 * @email: 395593455@qq.com
 * @LastEditors: actor
 * @LastEditTime: 2023-12-19 10:13:49
 * @FilePath: \i-hos-app\shared\emr\src\components\common\Calendar.vue
 * @Description: 日历组件
-->
<template>
  <div class="calendar">
    <el-calendar ref="calendar" style="width: 273px; height: auto" v-model="dateVal" @click.native.prevent>
      <template #header="{ date }">
        <el-tooltip content="上一年" placement="top">
          <el-icon class="head_icon" @click="selectDate('prev-year')"><ArrowLeft /></el-icon>
        </el-tooltip>
        <el-tooltip content="上一月" placement="top">
          <el-icon class="head_icon" @click="selectDate('prev-month')"><DArrowLeft /></el-icon>
        </el-tooltip>
        <span class="head_date">{{ date }}</span>
        <el-tooltip content="下一月" placement="top">
          <el-icon class="head_icon" @click="selectDate('next-month')"><DArrowRight /></el-icon>
        </el-tooltip>
        <el-tooltip content="下一年" placement="top">
          <el-icon class="head_icon" @click="selectDate('next-year')"><ArrowRight /></el-icon>
        </el-tooltip>
      </template>
      <template #date-cell="{ data }">
        <p :class="data.isSelected ? 'is-selected' : ''">
          {{ data.day.split('-').slice(2).join('-') }}
          {{ data.isSelected ? '✔' : '' }}
        </p>
      </template>
    </el-calendar>
    <footer slot="footer" class="dialog-footer">
      <el-button @click="cancelData">取消</el-button>
      <el-button type="success" @click.native.prevent="selectDate('today')">今天</el-button>
      <el-button type="primary" @click="sureData">确定</el-button>
    </footer>
  </div>
</template>

<script lang="ts" setup>
  import { Ref, ref } from 'vue'
  // 接受父组件传的参数
  const props = defineProps<{
    init?: boolean
  }>()
  import { toRefs } from 'vue'
  // 接口约束
  import { IPageConfigStore } from '../../interface/page'
  // store数据
  import { usePageConfigStore } from '../../store/pageConfig'
  // 获取公共的hooks
  import { useCommon } from '../../hooks/useCommon'
  // 引入时间转换公共函数
  import { timestamp } from '../../utils/index'

  // 解构 store的配置数据, 获取光标的位置或选定对象
  const pageConfigStore: IPageConfigStore = usePageConfigStore()
  const { val } = toRefs<any>(pageConfigStore)
  // 获取设置属性
  const { setConfig } = useCommon()

  const calendar = ref<any>() // 初始化calendar
  const dateVal: Ref<Date> = ref(val.value.textVal ? val.value.textVal : new Date()) // 初始化日期
  // 选择改变日期
  const selectDate = (val: any): void => {
    if (!calendar.value) return
    calendar.value.selectDate(val)
  }

  // 定义向父组件传值的事件
  const $emit = defineEmits<{
    (e: 'toAddDate', dateVal: Ref<Date>): void
  }>()

  // 确定选择日期
  const sureData = (): void => {
    if (props.init) {
      $emit('toAddDate', dateVal)
    } else {
      // 修改配置仓库
      pageConfigStore.$patch?.((state: IPageConfigStore) => {
        state.val.textVal = timestamp(dateVal.value)
        ;(document.getElementById(state.el) as any).innerText = timestamp(dateVal.value)
        state.commonComponentActive = ''
        setConfig()
      })
    }
  }

  // 取消日期选择
  const cancelData = (): void => {
    // 修改配置仓库
    pageConfigStore.$patch?.((state: IPageConfigStore) => {
      state.commonComponentActive = ''
      setConfig()
    })
  }
</script>
<style scoped lang="scss">
  .calendar {
    box-shadow: 0 0 3px #ccc;
    padding: 10px;
    border-radius: 4px;
    background-color: #fff;
  }
  .head_date {
    width: 70%;
    text-align: center;
    cursor: initial;
  }
  p {
    padding: 0;
    margin: 0;
    text-align: center;
  }
  .is-selected {
    color: #1989fa;
    font-weight: bold;
  }
  :deep(.el-calendar__header) {
    align-items: center;
    font-size: 16px;
    padding: 10px;
  }
  :deep(.el-calendar-table thead th) {
    padding: 0;
    text-align: center;
  }
  :deep(.el-calendar__body) {
    padding: 10px 0;
    font-size: 14px;
  }
  :deep(.el-calendar-table .el-calendar-day) {
    height: auto;
    line-height: 1.5em;
  }
  :deep(.el-calendar-table .is-selected .el-calendar-day) {
    padding: 8px 0;
  }
  :deep(.el-calendar-table td.is-today) {
    p {
      background-color: #409eff;
      border-radius: 100%;
      color: #fff;
      font-weight: bold;
    }
    p.is-selected {
      color: #409eff;
      background: transparent;
    }
  }
</style>
